import flipClock from '../../components/payDate/FlipClock'
export default {
  data() {
    return {
      // Core,
      deposit_amount: '',
      payType: '',
      url: '',
      oid: '',
      yuer: 0,
      waittimer: '',
      isShow: true,
      dingj: '',
      stuas: 0,
      mShow6: false,
      mShowye: false,
      isSetPayPsd: false,
      yepsdStatus: false, //余额支付
      payPassword: '', //密码

    }
  },
  onLoad(option) {

    this.oid = option.id
    this.deposit_amount = option.deposit_amount
    this.waittimer = option.time
    console.log(this.waittimer)
    this.getTime()
    this.IsExistPayPwd()
  },
  components: {
    flipClock
  },
  // 监听返回按钮 记得return true
  onBackPress(options) {
    if (options.from === 'backbutton') {
      uni.switchTab({
        url: '/pages/me/me'
      })
      return true
    }
  },
  methods: {
    async IsExistPayPwd() {
      try {
        const res = await this.$api.user.IsExistPayPwd()
        console.log(res)
        this.isSetPayPsd = res.data
      } catch (e) {
        console.log(e)
        //TODO handle the exception
      }
    },
    // 不同的支付方式分开处理
    goType() {
      if (this.payType == 2) {
        if (this.isSetPayPsd) {
          this.yepsdStatus = true
        } else {
          uni.showToast({
            title: '请先设置支付密码',
            icon: 'none'
          })
          setTimeout(() => {
            uni.navigateTo({
              url: '/pages_common/pages_me/assets/setPassword/setPsd'
            })
          }, 1200)
        }

      } else {
        // setTimeout(function() {
        //   this.mShow6 = true
        // }, 2000)
        this.goPay()
      }

    },
    async goPay() {
      console.log(11111)
      // let data = {
      //   // this.oid
      //   'contractid': this.oid,
      //   'type': this.payType //type（1、微信 0、支付宝 2、余额）
      // }
      let data = {
        'orderId': this.oid,
        'payModule': 5,
        'payPlatform': {
          'payPlatformType': Number(this.payType),
          'payPlatformDetailType': 3,
        }
      }
      console.log(data)
      if (this.payType !== '') {

        let res = await this.$api.pay.OrderContractPay(data)
        try {
          // console.log('支付结果', res, this.payType)
          if (res.code === 200) {
            let payway = ''
            if (this.payType === '0') {
						  this.url = res.data.data
						  payway = 'alipay'
            } else {
						  let data = JSON.parse(res.data.data.parameter)
						  this.url = {
						    appid: data.appid,
						    noncestr: data.noncestr,
						    package: data.package,
						    partnerid: data.partnerid,
						    prepayid: data.prepayid,
						    sign: data.sign,
						    timestamp: data.timestamp,
						  }
						  payway = 'wxpay'
            }
            console.log(111, this.url)

            uni.requestPayment({
						  provider: payway,
						  orderInfo: this.url,
						  success: function(res) {
						    console.log('success:', res)
						    // this.mShow6 = true
						    //         uni.redirectTo({

						  },
						  fail: function(err) {
						    console.log('err:', err)
						  },
						  complete: () => {
						    // console.log('执行了:')
						    uni.redirectTo({
						     url: '/pages_common/pages_me/paySuccess/paySuccess?type=' + this.payType +
						     	'&id=' + this.oid + '&paytype=0'
						    })

								 // // type 支付方式 0支付宝 1微信  paytype支付类型 0订单 1保证金
						    // this.mShow6 = true
						  }
            })

            // this.url = res.data
            // var abc = this.url.substring(this.url.indexOf('?') + 2)
            // if (this.payType === '0') {
            // 	// 分解参数 跳转页面 跳转到webview页面
            // 	let id = this.url.split('prepay_id=')[1].split('&package=')[0]
            // 	let pack = this.url.split('&package=')[1]
            // 	uni.navigateTo({
            // 		url: '/pages_order/payLoading/payLoading?paytype=0&package=' +
            // 			pack + '&id=' + id + '&dealId=' + this.oid
            // 	})

            // } else if (this.payType === '1') {
            // 	// 调起支付宝支付 orderInfo为string类型  微信为object类型
            // 	uni.requestPayment({
            // 		provider: 'alipay',
            // 		orderInfo: res.data,
            // 		success: function(res) {
            // 			console.log('success:', res)
            // 		},
            // 		fail: function(err) {
            // 			console.log('fail:', res)
            // 			this.mShow6 = false
            // 		},
            // 		complete: () => {
            // 			this.mShow6 = true
            // 		}
            // 	})

            // } else {
            // 	this.mShowye = false
            // 	this.confirmHE()
            // }
          }
        } catch (err) {
          console.log(err)
        }
      } else {
        uni.showToast({
          title: '请先选择支付方式',
          icon: 'none',
        })
      }
    },
    finish(e) {
      console.log(e)
      this.payPassword = e
    },
    async ClientBalancePay() {

      if (this.payPassword.length === 6) {
        let data = {
          id: this.oid,
          userInputPayPassword: this.payPassword,
          payModule: 5 //购车--2 维保--3 福利购--4  车辆买卖--5
        }
        try {
          const res = await this.$api.common.BusBalancePay(data)
          console.log('余额支付', res)
          uni.showToast({
            title: '支付成功！',
            icon: 'success'
          })
          this.paypsd = false
          setTimeout(() => {
            uni.navigateBack(2)
          }, 1200)
        } catch (err) {
          console.log('支付失败', err)
          this.payPassword = ''
        }
      } else {
        uni.showToast({
          title: '请输入正确的支付密码',
          icon: 'none'
        })
      }
    },
    call(phone) {
      AddMobileContactData(this.$store.state.userInfoDetail.store.storeId)
      uni.makePhoneCall({
        phoneNumber: phone //仅为示例
      })
    },

    radioChange(val) {
      this.payType = val.detail.value
    },
    getTime() {
      // var parmeter = {
      // 	contractid: this.oid,
      // 	userid: Core.CurrentUser().uid
      // };
      // Core.AjaxPost(Core.Bus.QueryTime, parmeter, res => {
      // 	this.timer = res.data.Item.dateDiff
      // 	this.yuer = res.data.Item.balance
      // 	this.dingj = res.data.Item.depositamount
      // 	this.isShow = true
      // })
    },
    // 余额
    confirmYe() {
      console.log(1111, '余额')
      this.goPay()
    },
    // 支付宝微信
    confirmHE() {
      uni.redirectTo({
        // type 支付方式  paytype支付类型 0订单 1保证金
        url: '/pages_common/pages_me/paySuccess/paySuccess?type=' + this.payType +
					'&id=' + this.oid + '&paytype=0'
      })
    }
  }
}
